﻿using System;
using System.Web.UI;
using DotConferenceSql;
using DotConferenceCore.Logistique;
using System.Data.SqlClient;

namespace WebInterface
{
    public partial class CreationConference : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ButtonCreerConference_Click(object sender, EventArgs e)
        {
            if( Page.IsValid )
            {
                var nouvelleConference = new Conference(CalendarDateDebut.SelectedDate, CalendarDateFin.SelectedDate)
                {
                    Nom = TextNom.Text,
                    Ville = TextVille.Text,
                    Lieu = TextLieu.Text,
                    MinimumParticipant = Convert.ToInt32(TextParticipantMinimal.Text),
                    MaximumParticipant = Convert.ToInt32(TextParticipantMaximal.Text),
                    CoutMinimal = Convert.ToDecimal(TextCoutMinimal.Text),
                    DateLimiteInscription = CalendarDateLimiteInscription.SelectedDate,
                    DateLimiteReceptionArticle = CalendarDateLimiteArticle.SelectedDate,
                    DateLimiteRapportEvaluation =
                        CalendarDateLimiteEvaluation.SelectedDate,
                    DateLimiteReceptionArticleCorrige =
                        CalendarDateLimiteArticleCorrige.SelectedDate
                };

                using (var query = new SqlQuery())
                {
                    var insertElement = new InsertIntoQueryElement();
                    insertElement.Table = "Conference";

                    insertElement.Add("nom", nouvelleConference.Nom);
                    insertElement.Add("ville", nouvelleConference.Ville);
                    insertElement.Add("lieu", nouvelleConference.Lieu);
                    insertElement.Add("maximumParticipant", nouvelleConference.MaximumParticipant);
                    insertElement.Add("minimumParticipant", nouvelleConference.MinimumParticipant);
                    insertElement.Add("dateDebut", nouvelleConference.DateHeureDebut);
                    insertElement.Add("dateFin", nouvelleConference.DateHeureFin);
                    insertElement.Add("coutMinimal", nouvelleConference.CoutMinimal);
                    insertElement.Add("dateLimiteInscription", nouvelleConference.DateLimiteInscription);
                    insertElement.Add("dateLimiteReceptionArticle", nouvelleConference.DateLimiteReceptionArticle);
                    insertElement.Add("dateLimiteRapportEvaluation", nouvelleConference.DateLimiteRapportEvaluation);
                    insertElement.Add("dateLimiteReceptionArticleCorrige", nouvelleConference.DateLimiteReceptionArticleCorrige);

                    query.Elements.Add(insertElement);

                    var insertCommand = query.BuildSqlCommand();

                    try
                    {
                        insertCommand.ExecuteNonQuery();
                        Response.Redirect("~/Default.aspx");
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.ToString());
                    }
                }
            }
        }
    }
}
